package com.test.pattern.combination;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Menu
 * @description: 菜单类(树枝节点角色)
 * @author: Mr du
 * @create: 2024-02-25 15:39
 */
public class Menu extends MenuComponent{

    //菜单可以有多个子菜单或者子菜单项
    private List<MenuComponent> menuComponentList;

    public Menu(String name,int level){
        super.name = name;
        super.level = level;
        menuComponentList = new ArrayList<>();
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponentList.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponentList.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponentList.get(index);
    }

    @Override
    public void print() {
        for (int i = 1; i < level; i++) {
            System.out.print("--");
        }
        System.out.println(name);
        for (MenuComponent menuComponent : menuComponentList) {
            menuComponent.print();
        }
    }
}
